package com.gxar.quick.ar.server.db.service;

import com.gxar.common.entity.login.UserProfile;
import com.gxar.common.utils.DateTimeUtils;
import com.gxar.common.utils.SnowFlakeUtils;
import com.gxar.common.utils.StringUtils;
import com.gxar.quick.ar.server.base.common.enums.TransactionStatusEnum;
import com.gxar.quick.ar.server.base.model.transaction.vo.request.CreateTransactionOrderRequest;
import com.gxar.quick.ar.server.db.mapper.TransactionOrderMapper;
import com.gxar.quick.ar.server.db.model.TransactionOrderEntity;
import com.gxar.quick.ar.server.db.model.TransactionWechatEntity;
import com.gxar.quick.ar.server.db.repository.TransactionOrderRepository;
import com.gxar.quick.ar.server.db.repository.TransactionWechatRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/6 17:59
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TransactionService {

    private final TransactionOrderRepository transactionOrderRepository;

    private final TransactionWechatRepository transactionWechatRepository;

    private final TransactionOrderMapper transactionOrderMapper;

    public TransactionOrderEntity createTransactionOrder(UserProfile userProfile,
            CreateTransactionOrderRequest request) {
        TransactionOrderEntity transactionOrderEntity = new TransactionOrderEntity();
        String transactionId = SnowFlakeUtils.getInstance().nextId();
        transactionOrderEntity.setId(Long.valueOf(transactionId));
        transactionOrderEntity.setPlatform(request.getPlatform());
        transactionOrderEntity.setSource(request.getSource());
        transactionOrderEntity.setBusinessId(request.getOrderId());
        transactionOrderEntity.setType(request.getType());
        transactionOrderEntity.setStatus(TransactionStatusEnum.TRANSACTION_DEALING.getCode());
        transactionOrderEntity.setPrice(request.getPrice());
        transactionOrderEntity.setCreatedAt(new Date());
        transactionOrderEntity.setUpdatedAt(transactionOrderEntity.getCreatedAt());
        transactionOrderEntity.setCreator(userProfile.getMobile());
        transactionOrderEntity.setOperator(userProfile.getMobile());
        //支付单半小时后过期
        transactionOrderEntity.setTimeExpire(DateTimeUtils.addMinutes(transactionOrderEntity.getCreatedAt(), 30));
        return transactionOrderRepository.save(transactionOrderEntity);
    }

    public void updateTransactionOrderById(Long transactionId, String extTradeNo, String failReason) {
        Optional<TransactionOrderEntity> transactionOrderOption = transactionOrderRepository.findById(transactionId);
        if (transactionOrderOption.isPresent()) {
            TransactionOrderEntity transactionOrderEntity = transactionOrderOption.get();
            transactionOrderEntity.setExtTradeNo(extTradeNo);
            if (StringUtils.isNotBlank(failReason)) {
                transactionOrderEntity.setReason(failReason);
                transactionOrderEntity.setStatus(TransactionStatusEnum.TRANSACTION_FAILED.getCode());
            } else {
                transactionOrderEntity.setStatus(TransactionStatusEnum.TRANSACTION_COMPLETE.getCode());
            }
            transactionOrderRepository.save(transactionOrderEntity);
        }
    }

    public void createTransactionWechat(TransactionWechatEntity transactionWechatEntity) {
        transactionWechatRepository.save(transactionWechatEntity);
    }

    public List<TransactionOrderEntity> queryExpiredTransaction(Date expireTime) {
        return transactionOrderMapper.queryExpiredTransaction(expireTime);
    }

    public void updateTransactionStatusById(Long transactionId, TransactionStatusEnum transactionStatusEnum) {
        Optional<TransactionOrderEntity> transactionOption = transactionOrderRepository.findById(transactionId);
        if (transactionOption.isPresent()) {
            TransactionOrderEntity transaction = transactionOption.get();
            transaction.setStatus(transactionStatusEnum.getCode());
            transaction.setUpdatedAt(new Date());
            transactionOrderRepository.save(transaction);
        }
    }
}
